﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation
{
    using System;
    using System.Windows.Forms;
    using ConnectBasic.Shell.Extensibility;
    using ConnectDebug;

    internal partial class LocalsToolWindow : UserControl
    {
        internal const string InvariantName = "Locals";

        private IDebugger mDebugger;

        public LocalsToolWindow(IShell shell)
        {
            InitializeComponent();

            // Get hold of the debugger
            IDebuggerService svc = shell.GetService<IDebuggerService>();

            if (svc != null)
            {
                mDebugger = svc.Debugger;
                mDebugger.BreakpointHit += new System.EventHandler(Debugger_BreakpointHit);
            }
        }

        void Debugger_BreakpointHit(object sender, System.EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new EventHandler(Debugger_BreakpointHit), sender, e);
            }
            else
            {
                // Update locals
                LocalsList.BeginUpdate();
                LocalsList.Items.Clear();

                foreach (var local in mDebugger.CurrentPosition.Parameters)
                {

                    // Make sure its not a hidden parameter
                    if (local.Name == null || local.Name.StartsWith("#"))
                    {
                        continue;
                    }

                    // Create the listview item and get the parameter
                    // value on a different thread (can't call
                    // debugger methods from STA threads)
                    var lvi = new ListViewItem(local.Name);

                    System.Threading.ThreadPool.QueueUserWorkItem(
                        delegate(object state)
                        {
                            var value = mDebugger.GetParameterValue(((DebugParameterInfo)state).Handle);

                            this.Invoke(new System.Action(delegate()
                            {
                                lvi.SubItems.Add(value == null ? "Nothing" : value.ToString());
                            }));
                        },
                        local
                    );

                    LocalsList.Items.Add(lvi);
                }

                LocalsList.EndUpdate();
            }
        }
    }
}
